﻿<%@ Page Language="C#" EnableEventValidation="false" %>

<%@ Register Assembly="FreeTextBox" Namespace="FreeTextBoxControls" TagPrefix="FTB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="TarreOA.TarreOAModels" %>
<%@ Import Namespace="TarreOA.TarreOABLL" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">

    private static int fcid;
    
    protected void dltAffixFiles_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string fileId = ((HtmlInputHidden)e.Item.FindControl("hidFileId")).Value;
            string filePath = ((HtmlInputHidden)e.Item.FindControl("hidFilePath")).Value;
            string fileName = ((HyperLink)e.Item.FindControl("hlkAffixFile")).Text; 
            ((Button)e.Item.FindControl("Button2")).Attributes.Add("FilePath", filePath);
            ((Button)e.Item.FindControl("Button2")).Attributes.Add("FileName", fileName);   
        }  
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            if(Request["fcid"] != null)
            {
                fcid = int.Parse(Request["fcid"]);
                this.DataList1_DataTable();
                ViewState["fcid"] = fcid;
            }
        } 
    }

    protected void DataList1_DataTable()
    {
        User curLoginUser = (User)Session["user"];
        UserFileCabinettbService userFileCabinettbService = new UserFileCabinettbService();
        UserFileCabinettb userFileCabinet = userFileCabinettbService.findByFileId(fcid);
        this.lblRigisterTime.Text = userFileCabinet.FileUploadTime.ToString();
        this.lblFileTitle.Text = userFileCabinet.FileTitle;
        this.ftbFileContent.Text = userFileCabinet.FileContent;
        UserFileCabinetUploadFiletbService userFileCabinetUploadFiletbService = new UserFileCabinetUploadFiletbService();
        List<UserFileCabinetUploadFile> list = userFileCabinetUploadFiletbService.findByFileCabinetId(fcid);
        if (list != null && list.Count > 0)
        {
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            dt.Columns.Add("FileName", typeof(string));
            dt.Columns.Add("FilePath", typeof(string));
            dt.Columns.Add("FileId", typeof(string));
            foreach (UserFileCabinetUploadFile userFileCabinetUploadFile in list)
            {
                DataRow dr = dt.NewRow();
                dr["FileName"] = userFileCabinetUploadFile.FileName;
                dr["FilePath"] = userFileCabinetUploadFile.FilePath.Replace(FindConfigurationPath.instance().UserFileCabinetPaths[curLoginUser.LoginName].ToString(), "");
                dr["FileId"] = userFileCabinetUploadFile.FileId.ToString();
                dt.Rows.Add(dr);
            }
            ds.Tables.Add(dt);
            this.dltAffixFiles.DataSource = ds.Tables[0].DefaultView;
            this.dltAffixFiles.DataBind();
        }
        else
        {
            this.dltAffixFiles.DataSource = null;
            this.dltAffixFiles.DataBind(); 
        }   
         
    }

    //protected void btnExport_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect("http://tv.tarre.com.cn");
    //}

    protected void Button2_Click(object sender, EventArgs e)
    {
        User curLoginUser = (User)Session["user"];
        string filePath = FindConfigurationPath.instance().UserFileCabinetPaths[curLoginUser.LoginName].ToString() +
            ((Button)sender).Attributes["FilePath"];
        string fileName = ((Button)sender).Attributes["FileName"];
        FileInfo fi = new FileInfo(filePath);
        if (fi.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment;fileName=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.ContentType = "application/octet-stream";
            Response.Filter.Close();
            Response.WriteFile(fi.FullName);
            Response.End();
        }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (ViewState["fcid"] != null)
        {
            fcid = (int)ViewState["fcid"]; 
        }  
        UserFileCabinettbService userFileCabinettbService = new UserFileCabinettbService();
        UserFileCabinetUploadFiletbService userFileCabinetUploadFiletbService = new UserFileCabinetUploadFiletbService();
        List<UserFileCabinetUploadFile> list = userFileCabinetUploadFiletbService.findByFileCabinetId(fcid);
        if(list != null && list.Count > 0)
        {
            foreach(UserFileCabinetUploadFile userFileCabinetUploadFile in list)
            {
                File.Delete(userFileCabinetUploadFile.FilePath); 
            }   
        }
        userFileCabinettbService.delFileCabinetInfoByFileId(new string[] { fcid.ToString()});
        ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "window.close();", true);
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <base target="dl" />
    <script type="text/javascript" src="../js/FileCabinet.js"></script>
    <script type="text/javascript" src="../js/MyOption.js"></script>
</head>
<body bgcolor="#EEEEEE" >
    <iframe id="dl" name="dl" height="0" width="0"></iframe>
    <form id="form1" runat="server" >
    <div>
        <table align="left" style="width: 549px; height: 445px">
            <tr>
                <td colspan="3" style="width: 4253px; height: 23px">
                    <asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Smaller"
                        Height="21px" Text="创建时间：" Width="72px"></asp:Label>
                    <asp:Label ID="lblRigisterTime" runat="server" Font-Bold="True" Font-Size="Smaller"
                        ForeColor="#00C0C0" Height="19px" Width="282px"></asp:Label><input id="Button1" onclick="closeThisWindow('chkCur')"
                            style="width: 90px" type="button" value="关闭窗口" /><asp:Button ID="btnDel" runat="server"
                                OnClick="btnDel_Click" Text="删除文件" Width="94px" /></td>
            </tr>
            <tr>
                <td colspan="3" style="width: 4253px; height: 23px">
                    <asp:Label ID="Label2" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Smaller"
                        Height="21px" Text="文件标题：" Width="72px"></asp:Label>
                    <asp:Label ID="lblFileTitle" runat="server" Font-Bold="True" Font-Size="Smaller"
                        ForeColor="#00C0C0" Height="19px" Width="463px"></asp:Label></td>
            </tr>
            <tr>
                <td colspan="3" style="width: 4253px">
                    <asp:Label ID="Label3" runat="server" Font-Bold="True" Font-Italic="True" Font-Size="Smaller"
                        Height="21px" Text="文件描述和附件：" Width="152px"></asp:Label></td>
            </tr>
            <tr>
                <td colspan="3" style="width: 4253px; height: 271px" valign="top">
                    <asp:DataList ID="dltAffixFiles" runat="server" BackColor="White" BorderColor="White"
                        BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" Height="41px"
                        OnItemDataBound="dltAffixFiles_ItemDataBound" Width="544px">
                        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
                        <SelectedItemStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
                        <ItemTemplate>
                            <input id="chkCur" name="chkCur" type="checkbox" value='<%# Eval("FileId") %>' />
                            <asp:HyperLink ID="hlkAffixFile" runat="server" Font-Bold="True" Font-Italic="True"
                                Font-Size="Smaller" ForeColor="#404040" Text='<%# Eval("FileName") %>' Width="436px"></asp:HyperLink>&nbsp;
                            <asp:Button ID="Button2" runat="server" Height="24px" OnClick="Button2_Click" Text="下载"
                                Width="57px" />
                            <input id="hidFilePath" runat="server" style="width: 85px" type="hidden" value='<%# Eval("FilePath") %>' />
                            <input id="hidFileId" runat="server" style="width: 63px" type="hidden" value='<%# Eval("FileId") %>' />
                            <input id="hidFileName" runat="server" style="width: 56px" type="hidden" />
                        </ItemTemplate>
                        <ItemStyle BackColor="#DEDFDE" ForeColor="Black" />
                        <HeaderTemplate>
                            <input id="chkAll" onclick="allMsgCheck('chkCur')" type="checkbox" />全选
                        </HeaderTemplate>
                        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
                        <FooterTemplate>
                            <input id="btnFull" onclick="willDeleteFileId('chkCur')" style="width: 160px" type="button"
                                value="将选中文件放入垃圾箱" />
                            <input id="btnRevert" disabled="disabled" onclick="revertDeleteFile('hidFids')" style="width: 160px"
                                type="button" value="还原删除文件" />
                        </FooterTemplate>
                    </asp:DataList><FTB:FreeTextBox ID="ftbFileContent" runat="server" Height="260px"
                        ReadOnly="True" Width="544px">
                    </FTB:FreeTextBox>
                </td>
            </tr>
        </table> 
        <input id="hidFids" type="hidden" />
        <asp:Panel ID="Panel1" runat="server" Height="445px" ScrollBars="Vertical" Width="570px">
        </asp:Panel> 
    </div>
    </form>
</body>
</html>
